"use strict";

require.config({
    waitSeconds: 0,
    paths: {
        'angular': 'angular/angular.min',
        'angular-aria': 'angular/angular-aria.min',
        'angular-animate': 'angular/angular-animate.min',
        'angular-messages': 'angular/angular-messages.min',
        'angular-material': 'angular/angular-material.min',
		'angular-file-upload': 'angular/ng-file-upload.min',
        'angular-file-upload-shim': 'angular/ng-file-upload-shim.min'
    },
    shim: {
        'angular' : { 'exports': 'angular' },
        'angular-aria': [ 'angular' ],
        'angular-animate': [ 'angular' ],
        'angular-messages': [ 'angular' ],
        'angular-material': [ 'angular', 'angular-aria', 'angular-animate', 'angular-messages' ],
		'angular-file-upload': [ 'angular', 'angular-file-upload-shim' ]
    },
    priority: [ 'api', 'app', 'angular' ],
});

// two-staged require
// so that login check will be earlier
require(['api', 'app'], (make_api, app) => {
    // init api
    var api = make_api();
    if (api == null) return;
    api.user_validate(() => {}); // validate user so that user's time may not be wasted.

    require(['angular', 'angular-material', 'angular-file-upload'], (angular) => {
        // fuck ARIA off
        (warn =>
            console.warn = x => (x.startsWith('ARIA:') ? null : warn(x))
        )(console.warn);

        // init app
        angular.element(document.querySelector('html')).ready(() => {
            app(angular, api);
            angular.bootstrap(document, ['rsms']);

            // hide preloading
            var preloading = document.querySelector('#preloading');
            preloading.parentElement.removeChild(preloading);
        });

        // TODO: remove these on release
        window.api = api;
    });
});

